package com.zzb.framework.frameworkcontroller.interceptor;

import com.zzb.framework.common.AbstractWebInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理后台拦截器
 * Created by zhangzhenbin on 17-2-22.
 */
@Component
public class WebFrameworkInterceptor extends AbstractWebInterceptor {
    @Value("${autoLogin:false}")
    private Boolean autoLogin;

    private static final String SESSION_KEY = "FRAMEWORK_USER_SESSION";

    private static String redirectUrl = "/loginframework/login?redirect=%s";
    private static String [] urlPattern = new String[]{
            "/framework/*",
            "/framework",
    };

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String uri = httpServletRequest.getRequestURI();
        if(redirectUrl.contains(uri)){
            httpServletRequest.setAttribute("autoLogin",autoLogin);
        }
        return super.preHandle(httpServletRequest, httpServletResponse, o);
    }

    @Override
    public String[] getUrlPattern() {
        return urlPattern;
    }

    @Override
    public String getRedirectUrl() {
        return redirectUrl;
    }

    @Override
    public String getSessionKey() {
        return SESSION_KEY;
    }
}
